package country_setting

import (
	"errors"
	"fmt"
	"hft/Database/admin/country_setting"
	"hft/Http/Models/dao"
	"hft/Http/Services/country_setting/setting_dao"
	"hft/Http/Services/country_setting/setting_model"
	utils "hft/Http/Utils"
	"hft/e"
	"hft/global"
	"hft/tools"
)

type Emoji struct{}

// 获取Emoji列表
func (*Emoji) List(paginate *setting_model.EmojiSearch) (*utils.Page, error) {
	var emojiList = make([]*country_setting.Emoji, 0)
	global.OC_DB.Scopes(setting_dao.EmojiDao.List(paginate, global.OC_DB)).Find(&emojiList).Scopes(dao.GetCount(global.OC_DB)).Count(&paginate.Total)
	paginate.Data = emojiList
	return &paginate.Page, nil
}

// 创建emoji
func (*Emoji) Add(data *country_setting.Emoji) (err error) {
	fmt.Println(tools.JsonParse(data))
	if res := global.OC_DB.Where("text_show = ?", data.TextShow).FirstOrCreate(&data); res.RowsAffected == 0 || res.Error != nil {
		return errors.New(e.EmojiHasExist)
	} else {
		return nil
	}
}

func (*Emoji) Edit(data *country_setting.Emoji) (err error) {
	if res := global.OC_DB.Model(&country_setting.Emoji{}).Where("id = ?", data.ID).Updates(map[string]interface{}{
		"text_show": data.TextShow,
		"emoji":     data.Emoji,
		"sort":      data.Sort,
	}); res.RowsAffected == 0 {
		return errors.New(e.EmojiHasExist)
	} else if res.Error != nil {
		return errors.New(e.ErrorInfo)
	} else {
		return nil
	}
}

func (*Emoji) Delete(ids []int) (err error) {
	if err = global.OC_DB.Unscoped().Delete(&country_setting.Emoji{}, ids).Error; err != nil {
		return errors.New(e.ErrorInfo)
	} else {
		return nil
	}
}

var EmojiService = new(Emoji)
